package com.phrp.service.impl;

import com.phrp.common.ConstantNumber;
import com.phrp.exception.CustomizationException;
import com.phrp.util.returnResult.ReturnInfoEnum;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.phrp.common.ConstantEnum;
import com.phrp.dao.InspectionRecordDao;
import com.phrp.dao.ManagerUserDao;
import com.phrp.entity.InspectionRecord;
import com.phrp.entity.ReportRecord;
import com.phrp.dao.ReportRecordDao;
import com.phrp.entity.dto.ReportSearchDTO;
import com.phrp.entity.vo.PageData;
import com.phrp.entity.vo.ReportSearchDataVO;
import com.phrp.entity.vo.ReportSubmitDataVO;
import com.phrp.service.ReportRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-05-03
 */
@Service
public class ReportRecordServiceImpl extends ServiceImpl<ReportRecordDao, ReportRecord> implements ReportRecordService {

    @Autowired
    private ReportRecordDao reportRecordDao;

    @Autowired
    private InspectionRecordDao inspectionRecordDao;

    @Autowired
    private ManagerUserDao managerUserDao;

    /**
     * 根据条件搜索数据
     *
     * @param searchData
     * @return
     */
    @Override
    public ResponseResult searchReportData(ReportSearchDataVO searchData) {
        searchData.setStartPage((searchData.getCurrentPage() - 1) * searchData.getPageSize());

        // 将字符串转换为时间格式
        if (searchData.getReportTime().size() > 0) {
            ZonedDateTime time1 = ZonedDateTime.parse(searchData.getReportTime().get(0), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setReportStartTime(time1.toLocalDateTime());

            ZonedDateTime time2 = ZonedDateTime.parse(searchData.getReportTime().get(1), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setReportEndTime(time2.toLocalDateTime());
        }

        LinkedList<ReportSearchDTO> datas = reportRecordDao.selectBySearchData(searchData);
        Integer totalNum = reportRecordDao.getTotalNum(searchData);

        PageData<LinkedList<ReportSearchDTO>> pageData = new PageData<>();
        pageData.setData(datas);
        pageData.setTotalNumber(totalNum);

        return new ResponseResult(ReturnInfoEnum.SUCCESS, pageData);
    }

    /**
     * 上报审核结果
     * @param report
     * @return
     */
    @Override
    public ResponseResult submitAudit(ReportSubmitDataVO report) {
        UpdateWrapper<ReportRecord> reportUpdate = new UpdateWrapper<>();
        // 如果结果是处理上报，就修改数据库中的建议
        if(report.getAudit().equals(ConstantEnum.HANDLE_PROBLEM.getCode())){
            reportUpdate.eq("r_uuid",report.getRid())
                    .set("finding_audit",report.getAudit())
                    .set("audit_opinion",report.getAuditOpinion());
            int i = reportRecordDao.update(null, reportUpdate);
            if(i != 1){
                throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
            }
        }
        // 如果结果是通过，就修改巡查结果并修改巡查员的巡查量，还有修改状态
        if(report.getAudit().equals(ConstantEnum.PASS_AUDIT.getCode())){
            reportUpdate.eq("r_uuid",report.getRid())
                    .set("finding_audit",report.getAudit());
            int i = reportRecordDao.update(null, reportUpdate);
            if(i != 1){
                throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
            }

            UpdateWrapper<InspectionRecord> inspectionUpdate = new UpdateWrapper();
            inspectionUpdate.eq("i_uuid",report.getIid())
                    .set("inspection_result",report.getInspectionResult());
            int i1 = inspectionRecordDao.update(null, inspectionUpdate);
            if(i1 != 1){
                throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
            }

            int i2 = managerUserDao.updateInspectionNumberById(report.getMid(), ConstantNumber.inspectionMinus);
            if(i2 != 1){
                throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
            }
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }
}
